﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.IO;
using DataAccess;

namespace Spider
{
    namespace Dygod
    {
        /// <summary>
        /// 综艺详情页面
        /// </summary>
        public class ShowDetail : Spider
        {
            /// <summary>
            /// 开始抓取工作
            /// <returns>是否成功</returns>
            /// </summary>
            public override bool DoWork()
            {
                Public.WriteLog("----- ShowDetail DoWork -----");
                WebClient client = new WebClient();
                try
                {
                    Public.WriteLog(CurrentUrl.ToString());
                    string content = Encoding.GetEncoding("gb2312").GetString(client.DownloadData(CurrentUrl));
                    Public.WriteLog("ShowDetail DownloadData success");

                    // 基本信息
                    string name = Regex.Match(content, "(?<=《).+?(?=》)").Value;
                    if (name.Length <= 0)
                    {
                        Public.WriteLog("ShowDetail Name is null");
                        return false;
                    }
                    string director = Regex.Match(content, "(?<=◎导　　演　).+?(?=<)").Value;
                    string actor = Regex.Match(content, "(?<=◎主　　演　)(.|\r|\n)+?(?=◎简　　介)").Value;
                    string type = Regex.Match(content, "(?<=◎类　　别　).+?(?=<)").Value;
                    string region = Regex.Match(content, "(?<=◎产　　地　).+?(?=<)|(?<=◎国　　家　).+?(?=<)").Value;
                    string duration = Regex.Match(content, "(?<=◎片　　长　).+?(?=<)").Value;
                    string first_showtime = Regex.Match(content, "(?<=◎上映日期　).+?(?=<)").Value;
                    string introduction = Regex.Match(content, "◎简　　介(.|\r\n)+?(?=◎影片截图)").Value;
                    // 海报
                    byte[] poster_file_bytes = null;
                    string poster_url = Regex.Match(content, "(?<=<img alt=\"\" src=\").+?(?=\" style=\"width: 518px; height: 815px;\")").Value;
                    if (poster_url.Length > 0)
                    {
                        if (poster_url.IndexOf("http") >= 0)
                        {
                            poster_file_bytes = client.DownloadData(poster_url);
                        }
                        else
                        {
                            poster_file_bytes = client.DownloadData(HomeUrl + poster_url);
                        }
                    }
                    else
                    {
                        Public.WriteLog("ShowDetail poster_url is null");
                    }
                    // 截图
                    string screenshoot_url = Regex.Match(content, "(?<=><img alt=\"\" src=\").+?(?=\" style=\"width: 926px; height: 850px;\")").Value;
                    byte[] screenshoot_file_bytes = null;
                    if (screenshoot_url.Length > 0)
                    {
                        if (screenshoot_url.IndexOf("http") >= 0)
                        {
                            screenshoot_file_bytes = client.DownloadData(screenshoot_url);
                        }
                        else
                        {
                            screenshoot_file_bytes = client.DownloadData(HomeUrl + screenshoot_url);
                        }
                    }
                    else
                    {
                        Public.WriteLog("ShowDetail screenshoot_url is null");
                    }
                    // FTP下载链接
                    string ftp_url_json = null;
                    {
                        Regex regex = new Regex("ftp://.+?(?=\r\")|ftp://.+?(?=\")");
                        List<SrcObj> objList = new List<SrcObj>();
                        foreach (Match m in regex.Matches(content))
                        {
                            // jianpian的FTP链接是有名称的
                            SrcObj obj = new SrcObj() { Name = Regex.Match(content, "(?<=" + m.Value + "\">).+?(?=<)").Value, Url = m.Value };
                            objList.Add(obj);
                        }
                        ftp_url_json = JsonConvert.SerializeObject(objList);
                    }
                    // Magnet下载链接
                    string magnet_url_json = null;
                    {
                        Regex regex = new Regex("(?<=href=\")magnet:.+?(?=\r\")|(?<=href=\")magnet:.+?(?=\")");
                        List<SrcObj> objList = new List<SrcObj>();
                        int index = 1;
                        foreach (Match m in regex.Matches(content))
                        {
                            // Magnet的链接名称需要自己根据链接计算，否则使用名称+索引
                            int start_pos = m.Value.LastIndexOf("-");
                            if (start_pos < 0)
                            {
                                start_pos = m.Value.LastIndexOf("=");
                            }
                            string show_name;
                            if (start_pos < 20)
                            {
                                show_name = name + "-"+ index.ToString();
                            }
                            else
                            {
                                string left_name = m.Value.Substring(start_pos + 1);
                                int end_pos = left_name.IndexOf(".");
                                show_name = left_name.Substring(0, end_pos);
                            }

                            SrcObj obj = new SrcObj() { Name = show_name, Url = m.Value };
                            objList.Add(obj);
                        }
                        magnet_url_json = JsonConvert.SerializeObject(objList);
                    }

                    // 保存信息
                    DataWriter.Instance.Replace("MOVIE", name, director, actor, type, region, duration, first_showtime, introduction,
                        poster_file_bytes, screenshoot_file_bytes, ftp_url_json, magnet_url_json, HomeUrl.ToString());
                }
                catch (WebException e)
                {
                    Public.WriteLog("ShowDetail WebException: " + e.ToString());
                }
                catch
                {
                    Public.WriteLog("ShowDetail Exception: unknown");
                }
                return true;
            }
        }
    }
}
